// ==UserScript==
// @name           Web Thunder Helper
// @namespace      http://www.quchao.com/entry/web-thunder-helper/
// @author         Qu Chao (Chappell.Wat) <Chappell.Wat@Gmail.com>
// @description    To register some protocol handlers for web thunder
// @include        http://dynamic.lixian.vip.xunlei.com/user*
// @version        1.0.0
// ==/UserScript==
// ver 1.0 @ 2011-02-24
//  Initialize release

(function (win, doc) {
    // check compatibilities
    if (!('registerProtocolHandler' in win.navigator)) {
        return;
    }

    // for logon only
    var settingTplElm = doc.getElementById('setting_main_tpl');
    if (null === settingTplElm) {
        return;
    }
    
    // get user id
    var userId = doc.cookie.match('(?:^| )userid=([^;]*)(?:;|$)');
    if (null === userId) {
        return;
    }
    
    // protocol handler related
    const HANDLER_URL = 'http://dynamic.lixian.vip.xunlei.com/user_task?userid=' + userId[1] + '&furl=%s';
    const HANDLER_NAME = '\u8FC5\u96F7\u79BB\u7EBF\u4E0B\u8F7D';

    // supported protocols
    const PROTOCOLS = {
        'thunder'  : '\u8FC5\u96F7',
        'flashget' : '\u5FEB\u8F66',
        'qqdl'     : '\u65CB\u98CE',
        'magnet'   : '\u78C1\u529B',
        'ed2k'     : '\u7535\u9A74',
    };

    // create styles
    doc.getElementsByTagName('head')[0].appendChild(doc.createElement('style')).appendChild(doc.createTextNode(
        '.rw_col2 {margin-right: 10px;}.rw_col2 input {margin-right: 3px;}'
    ));

    // hack the tpl
    var optionHtmlArr = [],
        optionTpl = '<label class="rw_col2"><input type="checkbox" id="{id}_protocol_handler" name="protocol" checked="checked" />{name} ({id}:)</label>';
    for (let protocol in PROTOCOLS) {
        if (!PROTOCOLS.hasOwnProperty(protocol)) {
            continue;
        }
        optionHtmlArr.push(optionTpl.replace(/{(\w+)}/g, function (nul, key) {
            switch (key) {
            case 'id':
                return protocol;
            case 'name':
                return PROTOCOLS[protocol];
            }
        }));
    }
    var insertAnchor = '</div>\n       <div class="btnin">';
    settingTplElm.value = settingTplElm.value.replace(insertAnchor, '<div class="dotline mag01"></div>\
           <h3 class="icrwlist" style="background-position:0 -68px">\u5173\u8054\u534F\u8BAE\u5230' + HANDLER_NAME + ' Web \u7248</h3>\
            <ul>\
              <li>' + optionHtmlArr.join('') + '</li>\
              <li><a class="btn_nol" href="#" id="register_protocol_button">\u7ACB\u5373\u5173\u8054</a></li>\
              <li>\u6CE8\u610F\uFF1AWeb \u7248\u65E0\u6CD5\u76F4\u63A5\u53D6\u6D88\u6240\u5173\u8054\u7684\u534F\u8BAE\uFF0C\u8BF7\u5230\u6D4F\u89C8\u5668\u76F8\u5173\u8BBE\u7F6E\u4E2D\u624B\u52A8\u53D6\u6D88\u3002<a href="http://www.quchao.com/entry/web-thunder-helper/#unregister" target="_blank">\u70B9\u51FB\u8FD9\u91CC\u83B7\u53D6\u5E2E\u52A9</a>)</li>\
            </ul>' + insertAnchor);

    // action
    var settingElm = doc.getElementById('setting_main');
    settingElm.addEventListener('click', function (evt) {
        if ('register_protocol_button' !== evt.target.id) {
            return;
        }
        
        // register selected protocols
        for (let protocol in PROTOCOLS) {
            true === doc.getElementById(protocol + '_protocol_handler').checked && win.navigator.registerProtocolHandler(protocol, HANDLER_URL, HANDLER_NAME);
        }
    }, true);

})(window, document);
